package sjtu.ist.warehouse4ws.action.file;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.epc2bpmn.ParserEpc;
import sjtu.ist.warehouse4ws.model.CustDomainEpc;
import sjtu.ist.warehouse4ws.model.CustDomainmodel;
import sjtu.ist.warehouse4ws.model.CustDomainmodelattr;
import sjtu.ist.warehouse4ws.model.CustDomainmodelstate;
import sjtu.ist.warehouse4ws.model.CustEpcunit;
import sjtu.ist.warehouse4ws.model.CustEpcunits;
import sjtu.ist.warehouse4ws.model.CustFileinfo;
import sjtu.ist.warehouse4ws.model.CustFunctionpoint;
import sjtu.ist.warehouse4ws.model.CustFunctionpointEpc;
import sjtu.ist.warehouse4ws.model.CustFunctions;
import sjtu.ist.warehouse4ws.model.CustModelgroup;
import sjtu.ist.warehouse4ws.model.CustParameter;
import sjtu.ist.warehouse4ws.model.CustProcess;
import sjtu.ist.warehouse4ws.model.CustRole;
import sjtu.ist.warehouse4ws.model.CustRoleEpc;
import sjtu.ist.warehouse4ws.model.DomainEpc;
import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;
import sjtu.ist.warehouse4ws.model.Epcunit;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.FunctionpointEpc;
import sjtu.ist.warehouse4ws.model.Functions;
import sjtu.ist.warehouse4ws.model.Modelgroup;
import sjtu.ist.warehouse4ws.model.Parameter;
import sjtu.ist.warehouse4ws.model.Process;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.RoleEpc;
import sjtu.ist.warehouse4ws.service.CompanyService;
import sjtu.ist.warehouse4ws.service.DomainEpcService;
import sjtu.ist.warehouse4ws.service.DomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.DomainModelStateService;
import sjtu.ist.warehouse4ws.service.EpcUnitService;
import sjtu.ist.warehouse4ws.service.EpcunitsService;
import sjtu.ist.warehouse4ws.service.FileInfoService;
import sjtu.ist.warehouse4ws.service.FunctionpointEpcService;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.FunctionsService;
import sjtu.ist.warehouse4ws.service.ModelgroupService;
import sjtu.ist.warehouse4ws.service.ParameterService;
import sjtu.ist.warehouse4ws.service.ProcessService;
import sjtu.ist.warehouse4ws.service.RoleEpcService;
import sjtu.ist.warehouse4ws.service.RoleService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainEpcService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelStateService;
import sjtu.ist.warehouse4ws.service.cust.CustEpcUnitService;
import sjtu.ist.warehouse4ws.service.cust.CustEpcunitsService;
import sjtu.ist.warehouse4ws.service.cust.CustFileInfoService;
import sjtu.ist.warehouse4ws.service.cust.CustFunctionpointEpcService;
import sjtu.ist.warehouse4ws.service.cust.CustFunctionpointService;
import sjtu.ist.warehouse4ws.service.cust.CustFunctionsService;
import sjtu.ist.warehouse4ws.service.cust.CustModelgroupService;
import sjtu.ist.warehouse4ws.service.cust.CustParameterService;
import sjtu.ist.warehouse4ws.service.cust.CustProcessService;
import sjtu.ist.warehouse4ws.service.cust.CustRoleEpcService;
import sjtu.ist.warehouse4ws.service.cust.CustRoleService;
import sjtu.ist.warehouse4ws.tool.Constants;
import sjtu.ist.warehouse4ws.tool.MyUtils;
import sjtu.ist.warehouse4ws.tool.ParseDomainModel;
import sjtu.ist.warehouse4ws.tool.ParserFunctionpoint;
import sjtu.ist.warehouse4ws.tool.ParserProcess;
import sjtu.ist.warehouse4ws.tool.PropertyHelper;

import com.opensymphony.xwork2.ActionSupport;

public class FileInfoAction extends ActionSupport {

	private String id;
	private String fileName;
	private String project;
	private String department;
	private String version;
	private int type;

	private String epcUnitType;
	private String epcUnitName;
	private String epcUnitId;
	private String epcUnitGroupId;
	private String epcUnitUid;
	private String path="";
	private String fileId="";
	// cust service
	private CustFileInfoService custFileInfoService;
	private CustEpcunitsService custEpcunitsService;
	private CustEpcUnitService custEpcUnitService;

	// private CustPermissionService custPermissionService;
	// private CustPermissionresourcesService custPermissionresourcesService;
	//
	// private CustRoleService custRoleService;
	// private CustRolepermissionsService custRolepermissionsService;

	// private CustDomainModelAttributeService
	// custDomainModelAttributeServiceImpl;
	// private CustDomainModelService custDomainModelServiceImpl;
	// private CustDomainModelStateService custDomainModelStateService;

	private CustFunctionpointService custFunctionpointService;
	private CustParameterService custParameterService;

	private CustFunctionsService custFunctionsService;
	private CustProcessService custProcessService;

	// base model service
	private FileInfoService fileInfoService;
	private EpcunitsService epcunitsService;
	private EpcUnitService epcUnitService;

	// private PermissionService permissionService;
	// private PermissionresourcesService permissionresourcesService;
	//
	private RoleService roleService;
	private CustRoleService custRoleService;
	// private RolepermissionsService rolepermissionsService;

	private DomainModelAttributeService domainModelAttributeService;
	private DomainModelService domainModelService;
	private DomainModelStateService domainModelStateService;
	private CustDomainModelAttributeService custDomainModelAttributeService;
	private CustDomainModelService custDomainModelService;
	private CustDomainModelStateService custDomainModelStateService;

	private FunctionpointService functionpointService;
	private ParameterService parameterService;
	private ModelgroupService modelgroupService;
	private CustModelgroupService custModelgroupService;

	public void setCustModelgroupService(
			CustModelgroupService custModelgroupService) {
		this.custModelgroupService = custModelgroupService;
	}

	private FunctionsService functionsService;
	private ProcessService processService;
	private FunctionpointEpcService functionpointEpcService;
	private CustFunctionpointEpcService custFunctionpointEpcService;
	private DomainEpcService domainEpcService;
	private CustDomainEpcService custDomainEpcService;

	private RoleEpcService roleEpcService;
	private CustRoleEpcService custRoleEpcService;

	private boolean success = false;

	private String modelName;

	public String getEpcUnitGroupId() {
		return epcUnitGroupId;
	}

	public void setEpcUnitGroupId(String epcUnitGroupId) {
		this.epcUnitGroupId = epcUnitGroupId;
	}

	static List<Epcunits> epcunitList = new ArrayList<Epcunits>();
	static List<Domainmodelstate> domainList = new ArrayList<Domainmodelstate>();
	static List<Functionpoint> functionList = new ArrayList<Functionpoint>();

	static List<CustEpcunits> custEpcunitList = new ArrayList<CustEpcunits>();
	static List<CustDomainmodelstate> custDomainList = new ArrayList<CustDomainmodelstate>();
	static List<CustFunctionpoint> custFunctionList = new ArrayList<CustFunctionpoint>();

	public List<Epcunits> eList = new ArrayList<Epcunits>();
	public List<CustEpcunits> custeList = new ArrayList<CustEpcunits>();

	// public List<Domainmodelstate> dmList=new ArrayList<Domainmodelstate>();
	// public List<Functionpoint> fpList=new ArrayList<Functionpoint>();

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public static List<Epcunits> getEpcunitList() {
		return epcunitList;
	}

	public static void setEpcunitList(List<Epcunits> epcunitList) {
		FileInfoAction.epcunitList = epcunitList;
	}

	public static List<CustEpcunits> getCustEpcunitList() {
		return custEpcunitList;
	}

	public static void setCustEpcunitList(List<CustEpcunits> custEpcunitList) {
		FileInfoAction.custEpcunitList = custEpcunitList;
	}

	public static List<CustDomainmodelstate> getCustDomainList() {
		return custDomainList;
	}

	public static void setCustDomainList(
			List<CustDomainmodelstate> custDomainList) {
		FileInfoAction.custDomainList = custDomainList;
	}

	public static List<Domainmodelstate> getDomainList() {
		return domainList;
	}

	public static void setDomainList(List<Domainmodelstate> domainList) {
		FileInfoAction.domainList = domainList;
	}

	public static List<Functionpoint> getFunctionList() {
		return functionList;
	}

	public static void setFunctionList(List<Functionpoint> functionList) {
		FileInfoAction.functionList = functionList;
	}

	// public void setPrivilegeService(PrivilegeService privilegeService) {
	// this.privilegeService = privilegeService;
	// }
	//
	// public void setOplogService(OplogService oplogService) {
	// this.oplogService = oplogService;
	// }

	public static List<CustFunctionpoint> getCustFunctionList() {
		return custFunctionList;
	}

	public static void setCustFunctionList(
			List<CustFunctionpoint> custFunctionList) {
		FileInfoAction.custFunctionList = custFunctionList;
	}

	public DomainEpcService getDomainEpcService() {
		return domainEpcService;
	}

	public String getEpcUnitUid() {
		return epcUnitUid;
	}

	public void setEpcUnitUid(String epcUnitUid) {
		this.epcUnitUid = epcUnitUid;
	}

	public void setRoleEpcService(RoleEpcService roleEpcService) {
		this.roleEpcService = roleEpcService;
	}

	public CustRoleEpcService getCustRoleEpcService() {
		return custRoleEpcService;
	}

	public void setCustRoleEpcService(CustRoleEpcService custRoleEpcService) {
		this.custRoleEpcService = custRoleEpcService;
	}

	public void setDomainEpcService(DomainEpcService domainEpcService) {
		this.domainEpcService = domainEpcService;
	}

	public CustDomainEpcService getCustDomainEpcService() {
		return custDomainEpcService;
	}

	public void setCustDomainEpcService(
			CustDomainEpcService custDomainEpcService) {
		this.custDomainEpcService = custDomainEpcService;
	}

	public FunctionpointEpcService getFunctionpointEpcService() {
		return functionpointEpcService;
	}

	public void setFunctionpointEpcService(
			FunctionpointEpcService functionpointEpcService) {
		this.functionpointEpcService = functionpointEpcService;
	}

	public CustFunctionpointEpcService getCustFunctionpointEpcService() {
		return custFunctionpointEpcService;
	}

	public void setCustFunctionpointEpcService(
			CustFunctionpointEpcService custFunctionpointEpcService) {
		this.custFunctionpointEpcService = custFunctionpointEpcService;
	}

	public String updateFileInfo() {
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin = currentUser.hasRole("superadmin");
		int companyId=admin?Constants.ADMIN_COMPANY_ID:1;
		// List<Fileinfo> list=fileInfoService.findFileInfoByName(fileName);
		// Fileinfo fileInfo=list.get(0);
		// admin=true;
		if (admin) {
			Fileinfo fileInfo = fileInfoService.getFileInfo(id,companyId);

			fileInfo.setProject(project);
			int groupInt;
			String groupId;
			String versionId;
//			List<Modelgroup> mgList = modelgroupService.getByName(project); // 查看是否是已存模型
//			if (mgList.toString() == "[]") {
//				Modelgroup modelgroup = new Modelgroup();
//
//				modelgroup.setGroupName(fileInfo.getProject());
//				modelgroupService.save(modelgroup);
//				mgList = modelgroupService.getByName(project);
//			}
			//groupInt = mgList.get(0).getId();
			groupId = fileInfo.getGroupId();
			versionId=fileInfo.getVersionId();
			
			fileInfo.setDepartment(department);
			fileInfo.setType(type);
			fileInfo.setVersionId(versionId);

			String path = fileInfo.getXmlpath();
			int in = path.lastIndexOf("\\");
			String temp = path.substring(0, in + 1);
			String newDir = temp.concat(getTypeById(type));
			String newPath = newDir + path.substring(in, path.length());

			// MyUtils.mkDirectory(newDir);
			// MyUtils.moveFile(path, newPath);
			fileInfo.setXmlpath(path);

			fileInfoService.updateFileInfo(fileInfo,companyId);
			
			parseById(type, path, id, groupId,versionId,companyId);
		} else {
			CustFileinfo fileInfo = custFileInfoService.getCustFileInfo(id);

			fileInfo.setProject(project);
			int groupInt;
			String groupId;
			String versionId;
			List<CustModelgroup> mgList = custModelgroupService
					.getByName(project); // 查看是否是已存模型
			if (mgList.toString() == "[]") {
				CustModelgroup modelgroup = new CustModelgroup();
				modelgroup.setGroupName(fileInfo.getProject());
				
				Modelgroup mg=new Modelgroup();
				PropertyHelper.copyProperties(modelgroup, mg);
				modelgroupService.save(mg,1);
				mgList = custModelgroupService.getByName(project);
			}
			groupInt = mgList.get(0).getId();
			groupId = groupInt + "";
			versionId=fileInfo.getVersionId();
			fileInfo.setGroupId(groupId);
			fileInfo.setDepartment(department);
			fileInfo.setType(type);
			fileInfo.setVersionId(versionId);

			String path = fileInfo.getXmlpath();
			int in = path.lastIndexOf("\\");
			String temp = path.substring(0, in + 1);
			String newDir = temp.concat(getTypeById(type));
			String newPath = newDir + path.substring(in, path.length());

			// MyUtils.mkDirectory(newDir);
			// MyUtils.moveFile(path, newPath);
			fileInfo.setXmlpath(path);

			custFileInfoService.updateCustFileInfo(fileInfo);
			
			parseByIdForCust(type, path, id, groupId,versionId);
		}

		success = true;
		return SUCCESS;
	}

	public String getEpcUnitId() {
		return epcUnitId;
	}

	public void setEpcUnitId(String epcUntiId) {
		this.epcUnitId = epcUntiId;
	}

	public String getEpcUnitType() {
		return epcUnitType;
	}

	public void setEpcUnitType(String epcUnitType) {
		this.epcUnitType = epcUnitType;
	}

	public String getEpcUnitName() {
		return epcUnitName;
	}

	public void setEpcUnitName(String epcUnitName) {
		this.epcUnitName = epcUnitName;
	}

	public String deleteFileInfo() {
		// if(!privilegeService.checkPrivilege(id, "delete")){
		// success=false;
		// return SUCCESS;
		// }

		CustFileinfo fileInfo = custFileInfoService.getCustFileInfo(id);
		MyUtils.deleteFile(fileInfo.getXmlpath());
		custFileInfoService.deleteCustFileInfo(id);
		success = true;

		return SUCCESS;
	}

	public String showEpcunit() {
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin = currentUser.hasRole("superadmin");
		if (admin) {
			List<Epcunits> temp = FileInfoAction.getEpcunitList();
			for (int i = 0; i < temp.size(); i++) {
				Epcunits e = new Epcunits();
				e.setName(epcunitList.get(i).getName());
				e.setId(epcunitList.get(i).getId());
				e.setEpcUnits(epcunitList.get(i).getEpcUnits());
				// e.setEpcUnits(null);
				eList.add(e);
				temp.remove(i);
			}
			FileInfoAction.setEpcunitList(temp);
			success = true;
		} else {
			List<CustEpcunits> temp = FileInfoAction.getCustEpcunitList();
			for (int i = 0; i < temp.size(); i++) {
				CustEpcunits e = new CustEpcunits();
				e.setName(custEpcunitList.get(i).getName());
				e.setId(custEpcunitList.get(i).getId());
				e.setCustEpcUnits(custEpcunitList.get(i).epcunitList());
				// e.setEpcUnits(null);
				custeList.add(e);
				temp.remove(i);
			}
			FileInfoAction.setCustEpcunitList(temp);

			this.eList.clear();
			for (CustEpcunits ce : custeList) {
				eList.add(ce);
			}
			success = true;
		}

		return SUCCESS;
	}

	public String showOtherModel() {
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin = currentUser.hasRole("superadmin");
		int companyId=admin?Constants.ADMIN_COMPANY_ID:1;
		if (admin) {
			if (epcUnitType.equals("function")) {
				for (Functionpoint f : FileInfoAction.getFunctionList()) {
					if (f.getEnName().equals(epcUnitName)) {
						FunctionpointEpc fpe = new FunctionpointEpc();
						fpe.setFunctionpoint(f.getId()); // 存储本来的Id
															// 依靠版本号来区分不同的模型版本
						fpe.setEpcUnit(epcUnitId);
						fpe.setEpcUid(epcUnitUid);
						fpe.setFunctionUid(f.getUid());
						functionpointEpcService.save(fpe,companyId);
						modelName = f.getCnName();
						break;
					}

				}
			} else if (epcUnitType.equals("event")) {
				for (Domainmodelstate d : FileInfoAction.getDomainList()) {
					if (d.getEnName().equals(epcUnitName)) {
						DomainEpc de = new DomainEpc();
						de.setDomain(d.getDomainModelid()); // 存储本来的Id
															// 依靠版本号来区分不同的模型版本
						de.setEpcUnit(epcUnitId);
						de.setEpcUid(epcUnitUid);
						de.setDomainUid(d.getUid());
						domainEpcService.save(de,companyId);
						modelName = d.getCnName();
						break;
					}

				}
			} else {

				List<Role> roleList = roleService
						.getRoleListbyGroupId(epcUnitGroupId,companyId);
				for (Role role : roleList) {
					if (role.getEnName().equals(epcUnitName)) {
						RoleEpc re = new RoleEpc();
						re.setEpcUnit(epcUnitId);
						re.setRole(role.getId());
						re.setRoleUid(role.getUid());
						re.setEpcUid(epcUnitUid);
						roleEpcService.save(re,companyId);
						modelName = role.getCnName();

						break;
					}
				}

			}
		} else {
			if (epcUnitType.equals("function")) {
				for (CustFunctionpoint f : FileInfoAction.getCustFunctionList()) {
					if (f.getEnName().equals(epcUnitName)) {
						CustFunctionpointEpc fpe = new CustFunctionpointEpc();
						fpe.setFunctionpoint(f.getId()); // 存储本来的Id
															// 依靠版本号来区分不同的模型版本
						fpe.setEpcUnit(epcUnitId);
						fpe.setEpcUid(epcUnitUid);
						fpe.setFunctionUid(f.getUid());
						custFunctionpointEpcService.save(fpe);
						modelName = f.getCnName();
						break;
					}

				}
			} else if (epcUnitType.equals("event")) {
				for (CustDomainmodelstate d : FileInfoAction
						.getCustDomainList()) {
					if (d.getEnName().equals(epcUnitName)) {
						CustDomainEpc de = new CustDomainEpc();
						de.setDomain(d.getDomainModelid()); // 存储本来的Id
															// 依靠版本号来区分不同的模型版本
						de.setEpcUnit(epcUnitId);
						de.setEpcUid(epcUnitUid);
						de.setDomainUid(d.getUid());
						custDomainEpcService.save(de);
						modelName = d.getCnName();
						break;
					}

				}
			} else {

				List<CustRole> roleList = custRoleService
						.getRoleListbyGroupId(epcUnitGroupId);
				for (CustRole role : roleList) {
					if (role.getEnName().equals(epcUnitName)) {
						CustRoleEpc re = new CustRoleEpc();
						re.setEpcUnit(epcUnitId);
						re.setRole(role.getId());
						re.setRoleUid(role.getUid());
						re.setEpcUid(epcUnitUid);
						custRoleEpcService.save(re);
						modelName = role.getCnName();

						break;
					}
				}

			}
		}

		success = true;
		return SUCCESS;
	}

	public String getTypeById(int type) {
		switch (type) {
		case 1:
			return "process";
		case 2:
			return "function";
		case 3:
			return "department";
		case 4:
			return "domain";
		case 5:
			return "epc";
		case 6:
			return "permission";
		case 7:
			return "role";
		default:
			return "";
		}
	}

	public void parseByIdForCust(int type, String path, String fileId,
			String groupId,String versionId) {
		switch (type) {
		case 1: {
			ParserProcess pp = new ParserProcess();
			Process process = pp.reserveProcess(path);
			process.setFileId(fileId);
			process.setUid(java.util.UUID.randomUUID().toString());
			process.setGroupId(groupId);
			process.setVersionId(versionId);
			ArrayList<Functions> fs = pp.reserveFunctions(path);

			custProcessService.save(new CustProcess(process));
			for (int i = 0; i < fs.size(); i++) {
				custFunctionsService.sava(new CustFunctions(fs.get(i)));
			}

			break;
		}

		case 2: {
			ParserFunctionpoint pfp = new ParserFunctionpoint();
			Functionpoint fp = pfp.reservePoint(path);
			fp.setFileId(fileId);
			fp.setUid(java.util.UUID.randomUUID().toString());
			fp.setGroupId(groupId);
			fp.setVersionId(versionId);
			FileInfoAction.getFunctionList().add(fp);
			ArrayList<Parameter> param = pfp.reserveParameter(path);
			custFunctionpointService
					.saveCustFunctionpoint(new CustFunctionpoint(fp));
			for (int i = 0; i < param.size(); i++) {
				param.get(i).setFileId(fileId);
				param.get(i).setGroupId(groupId);
				param.get(i).setUid(java.util.UUID.randomUUID().toString());
				param.get(i).setVersionId(versionId);
				custParameterService.save(new CustParameter(param.get(i)));
			}

			break;
		}

		case 3:

		case 4: {
			ParseDomainModel pdm = new ParseDomainModel();
			Domainmodel dmodel = pdm.reserveModel(path);
			dmodel.setFileId(fileId);
			dmodel.setUid(java.util.UUID.randomUUID().toString());
			dmodel.setGroupId(groupId);
			dmodel.setVersionId(versionId);
			ArrayList<Domainmodelattr> dmodelattr = pdm
					.reserveDomianmodelattr(path);
			ArrayList<Domainmodelstate> dmodelstate = pdm.reserveState(path);

			custDomainModelService.saveCustDomainModel(new CustDomainmodel(
					dmodel));
			for (int i = 0; i < dmodelattr.size(); i++) {
				dmodelattr.get(i)
						.setUid(java.util.UUID.randomUUID().toString());
				dmodelattr.get(i).setGroupId(groupId);
				dmodelattr.get(i).setVersionId(versionId);

				custDomainModelAttributeService
						.SaveCustDomainModelAttribute(new CustDomainmodelattr(
								dmodelattr.get(i)));
			}
			for (int i = 0; i < dmodelstate.size(); i++) {
				dmodelstate.get(i).setUid(
						java.util.UUID.randomUUID().toString());
				dmodelstate.get(i).setGroupId(groupId);
				dmodelstate.get(i).setVersionId(versionId);
				FileInfoAction.getDomainList().add(dmodelstate.get(i));
				custDomainModelStateService
						.savaCustDomainmodelstate(new CustDomainmodelstate(
								dmodelstate.get(i)));
			}
			break;
		}
		case 5: {
			try {
				// 解析后存入数据库
				List<CustEpcunits> epcunitsList = custEpcunitsService
						.getCustEpcunitsListById(fileId);
				if (epcunitsList.toString() == "[]") {
					CustEpcunits epcunits = ParserEpc.parseForCust(path);
					epcunits.setFileId(fileId);
					epcunits.setUid(java.util.UUID.randomUUID().toString());
					epcunits.setGroupId(groupId);
					epcunits.setName(epcunits.epcunitList().get(0).getName());
					epcunits.setId(fileId);
					epcunits.setVersionId(versionId);

					custEpcunitsService.saveCustEpcUnits(epcunits);
					int in = path.lastIndexOf("\\");

					FileInfoAction.getCustEpcunitList().add(epcunits);
					String epcId = fileId;
					List<CustEpcunit> list = epcunits.epcunitList();
					for (CustEpcunit epcunit : list) {
						epcunit.setEpcId(epcId);
						epcunit.setUid(java.util.UUID.randomUUID().toString());
						epcunit.setGroupId(groupId);
						epcunit.setVersionId(versionId);
						String eid = String.valueOf(epcunit.getId());
						String s = "";

						List<Integer> next = epcunit.getNext();

						for (int i : next) {
							s = s + i + ",";
						}
						epcunit.setNextstring(s);
						epcunit.setModelId(epcId + "_" + eid);
					}
					custEpcUnitService.addCustEpcUnit(list);
				}
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}

		}
	}

	public void parseById(int type, String path, String fileId, String groupId,String versionId,int companyId) {
		switch (type) {
		case 1: {
			ParserProcess pp = new ParserProcess();
			Process process = pp.reserveProcess(path);
			process.setFileId(fileId);
			process.setUid(java.util.UUID.randomUUID().toString());
			process.setGroupId(groupId);
			process.setVersionId(versionId);
			ArrayList<Functions> fs = pp.reserveFunctions(path);

			processService.save(process);
			for (int i = 0; i < fs.size(); i++) {
				functionsService.save(fs.get(i),companyId);
			}

			break;
		}

		case 2: {
			ParserFunctionpoint pfp = new ParserFunctionpoint();
			Functionpoint fp = pfp.reservePoint(path);
			fp.setFileId(fileId);
			fp.setUid(java.util.UUID.randomUUID().toString());
			fp.setGroupId(groupId);
			fp.setVersionId(versionId);
			FileInfoAction.getFunctionList().add(fp);
			ArrayList<Parameter> param = pfp.reserveParameter(path);
			functionpointService.saveFunctionpoint(fp,companyId);
			for (int i = 0; i < param.size(); i++) {
				param.get(i).setFileId(fileId);
				param.get(i).setGroupId(groupId);
				param.get(i).setUid(java.util.UUID.randomUUID().toString());
				param.get(i).setVersionId(versionId);
				parameterService.save(param.get(i));
			}

			break;
		}

		case 3:

		case 4: {
			ParseDomainModel pdm = new ParseDomainModel();
			Domainmodel dmodel = pdm.reserveModel(path);
			dmodel.setFileId(fileId);
			dmodel.setUid(java.util.UUID.randomUUID().toString());
			dmodel.setGroupId(groupId);
			dmodel.setVersionId(versionId);
			ArrayList<Domainmodelattr> dmodelattr = pdm
					.reserveDomianmodelattr(path);
			ArrayList<Domainmodelstate> dmodelstate = pdm.reserveState(path);

			domainModelService.saveDomainModel(dmodel,companyId);
			for (int i = 0; i < dmodelattr.size(); i++) {
				dmodelattr.get(i)
						.setUid(java.util.UUID.randomUUID().toString());
				dmodelattr.get(i).setGroupId(groupId);
				dmodelattr.get(i).setVersionId(versionId);

				domainModelAttributeService
						.saveDomainModelAttribute(dmodelattr.get(i),0);
			}
			for (int i = 0; i < dmodelstate.size(); i++) {
				dmodelstate.get(i).setUid(
						java.util.UUID.randomUUID().toString());
				dmodelstate.get(i).setGroupId(groupId);
				dmodelstate.get(i).setVersionId(versionId);
				FileInfoAction.getDomainList().add(dmodelstate.get(i));
				domainModelStateService
						.saveDomainmodelstate(dmodelstate.get(i),companyId);
			}
			break;
		}
		case 5: {
			try {
				// 解析后存入数据库
				List<Epcunits> epcunitsList = epcunitsService
						.getEpcunitsListById(fileId);
				if (epcunitsList.toString() == "[]") {
					Epcunits epcunits = ParserEpc.parse(path);
					epcunits.setFileId(fileId);
					epcunits.setUid(java.util.UUID.randomUUID().toString());
					epcunits.setGroupId(groupId);
					epcunits.setName(epcunits.getEpcUnits().get(0).getName());

					epcunits.setId(fileId);
					epcunits.setVersionId(versionId);
					epcunitsService.saveEpcUnits(epcunits,companyId);
					int in = path.lastIndexOf("\\");

					FileInfoAction.getEpcunitList().add(epcunits);
					String epcId = fileId;
					List<Epcunit> list = epcunits.getEpcUnits();
					for (Epcunit epcunit : list) {
						epcunit.setEpcId(epcId);
						epcunit.setUid(java.util.UUID.randomUUID().toString());
						epcunit.setGroupId(groupId);
						epcunit.setVersionId(versionId);
						String eid = String.valueOf(epcunit.getId());
						String s = "";

						List<Integer> next = epcunit.getNext();

						for (int i : next) {
							s = s + i + ",";
						}
						epcunit.setNextstring(s);
						epcunit.setModelId(epcId + "_" + eid);
					}
					epcUnitService.addEpcUnit(list,companyId);
				}
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}

		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getProject() {
		return project;
	}

	public void setProject(String project) {
		this.project = project;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	@JSON(name = "epcUnit")
	public List<Epcunits> getEList() {
		return this.eList;
	}

	@JSON(name = "totalEpcUnit")
	public int getTotalEpcUnit() {
		return this.eList.size();
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setCustFileInfoService(CustFileInfoService custFileInfoService) {
		this.custFileInfoService = custFileInfoService;
	}

	public void setCustEpcunitsService(CustEpcunitsService custEpcunitsService) {
		this.custEpcunitsService = custEpcunitsService;
	}

	public void setCustEpcUnitService(CustEpcUnitService custEpcUnitService) {
		this.custEpcUnitService = custEpcUnitService;
	}

	public void setCustFunctionpointService(
			CustFunctionpointService custFunctionpointService) {
		this.custFunctionpointService = custFunctionpointService;
	}

	public void setCustParameterService(
			CustParameterService custParameterService) {
		this.custParameterService = custParameterService;
	}

	public void setCustFunctionsService(
			CustFunctionsService custFunctionsService) {
		this.custFunctionsService = custFunctionsService;
	}

	public void setCustProcessService(CustProcessService custProcessService) {
		this.custProcessService = custProcessService;
	}

	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	public void setEpcunitsService(EpcunitsService epcunitsService) {
		this.epcunitsService = epcunitsService;
	}

	public void setEpcUnitService(EpcUnitService epcUnitService) {
		this.epcUnitService = epcUnitService;
	}

	public void setModelgroupService(ModelgroupService modelgroupService) {
		this.modelgroupService = modelgroupService;
	}

	// public void setPermissionService(PermissionService permissionService) {
	// this.permissionService = permissionService;
	// }
	//
	// public void setPermissionresourcesService(
	// PermissionresourcesService permissionresourcesService) {
	// this.permissionresourcesService = permissionresourcesService;
	// }
	//
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	// public void setRolepermissionsService(
	// RolepermissionsService rolepermissionsService) {
	// this.rolepermissionsService = rolepermissionsService;
	// }

	public void setCustRoleService(CustRoleService custRoleService) {
		this.custRoleService = custRoleService;
	}

	public void setDomainModelAttributeService(
			DomainModelAttributeService domainModelAttributeService) {
		this.domainModelAttributeService = domainModelAttributeService;
	}

	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}

	public void setDomainModelStateService(
			DomainModelStateService domainModelStateService) {
		this.domainModelStateService = domainModelStateService;
	}

	public void setCustDomainModelAttributeService(
			CustDomainModelAttributeService custDomainModelAttributeService) {
		this.custDomainModelAttributeService = custDomainModelAttributeService;
	}

	public void setCustDomainModelService(
			CustDomainModelService custDomainModelService) {
		this.custDomainModelService = custDomainModelService;
	}

	public void setCustDomainModelStateService(
			CustDomainModelStateService custDomainModelStateService) {
		this.custDomainModelStateService = custDomainModelStateService;
	}

	public void setFunctionpointService(
			FunctionpointService functionpointService) {
		this.functionpointService = functionpointService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setFunctionsService(FunctionsService functionsService) {
		this.functionsService = functionsService;
	}

	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	@JSON(name = "modelName")
	public String getModelName() {
		return modelName;
	}
	
	@JSON(name="path")
	public String getFilePath(){
		path=fileInfoService.getFileInfo(fileId,Constants.ADMIN_COMPANY_ID).getXmlpath();
		System.out.println(path);
		return SUCCESS;
	}
}
